<?php
/**
 *
 */

/**
 * iDaren的路由表
 */
$routing = array(
	'home'       => ABSPATH . DRINC . '/dr-page-question-list.php', // '/dr-page-home.php',
	'questions'  => ABSPATH . DRINC . '/dr-page-question-list.php', // '/dr-page-questions.php',
	'tags'       => ABSPATH . DRINC . '/dr-page-tag-list.php', //'/dr-page-tags.php',
	'users'      => ABSPATH . DRINC . '/dr-page-user-list.php', // '/dr-page-users.php',
	'badges'     => ABSPATH . DRINC . '/dr-page-badges.php',
	'unanswered' => ABSPATH . DRINC . '/dr-page-unanswered.php',
	'ask'        => ABSPATH . DRINC . '/dr-page-askquestion.php',

	'404'        => ABSPATH . DRINC . '/dr-page-404.php',

	'login'      => ABSPATH . DRINC . '/dr-page-login.php',
	'register'   => ABSPATH . DRINC . '/dr-page-register.php',

	'admin'      => ABSPATH . 'dr-admin/dr-page-admin-general-settings.php',
);


function parse_request_uri($uri) {
	$requests = array();

	if (strpos($uri, '?')) {
		list($path_str, $params_str) = explode('?', $uri, 2);
	} else {
		$path_str = $uri;
		$params_str = null;
	}


	$requests['orig_path']   = $path_str;
	$requests['paths']       = parse_request_path($path_str);

	return $requests;
}

/**
 * 将路径分隔成对应的数组
 * 
 * @param  $path
 * @return void
 */
function parse_request_path($path) {
	$paths = explode('/', $path);
	foreach ($paths as $key => $path) {
		if (empty($path)) {
			unset($paths[$key]);
		}
	}
	return array_values($paths);
}

/**
 * @return void
 */
function get_routing_file($requests) {
	global $routing;

	if (empty($requests['paths'])) {
		$path = 'home';
	} else {
		if ($requests['paths'][0] == 'index.php') {
			if (count($requests['paths']) == 1) {
				$path = 'home';
			} else {
				$path = $requests['paths'][1];
			}
		} else {
			$path = $requests['paths'][0];
		}
	}

	return isset($routing[$path]) ? $routing[$path] : $routing['404'];
}